<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Student;

/**
 * 班级
 *
 * @package app\api\controller
 */
class Classes extends Api
{
    protected $noNeedLogin = ['students'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiTitle    (学生列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/1/15)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/classes/students)
     * @ApiParams   (name="id", type="int", required=true, description="班级ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1579073877",
    "data": [
    {
    "id": 2369,
    "number": "20191008",
    "card_number": "55AA2254645644",
    "name": "薛凯琦",
    "pinyin": "wangxiaoer",
    "gender": 0,
    "birthday": "2005-03-12",
    "school_id": 36,
    "grade_id": 1,
    "class_id": 52,
    "created_at": 1574066510,
    "gender_text": "Female"
    }
    ]
    })
     */
    public function students()
    {
        $id = $this->request->request('id', 0);
        $keywords = $this->request->request('keywords', '');

        if (!$id) {
            $this->error('id 不能为空');
        }

        $list = Student::where('class_id', $id)
            ->where('name|number', 'like', "%$keywords%")
            ->where('status', Student::DISPLAY)
            ->field('id,number,card_number,name,pinyin,gender,avatar,birthday,school_id,grade_id,class_id,created_at')
            ->select();

        if ($list) {
            foreach ($list as $key => $datum) {
                if (!strstr($datum['avatar'], 'http') && strstr($datum['avatar'], '/uploads/')) {
                    $list[$key]['avatar'] = config('app_url') . $datum['avatar'];
                }
            }
        }

        $this->success('', $list);
    }

    /**
     * @ApiTitle    (班级信息)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/2/6)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/classes/profile)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="班级 ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1580971261",
    "data": {
    "id": 52,
    "name": "1班",
    "logo": "http://mp.uxuj.test/assets/images/class_default_logo.png",
    "slogan": null,
    "grade_id": 1,
    "grade_name": "一年级",
    "school_id": 36,
    "school_name": "房山长阳第一小学",
    "school_logo": "http://mp.uxuj.test/uploads/20191118/b4037c02a840db9dd1a78fa6ca932e1e.jpg",
    "classroom_id": 30,
    "classroom_name": "1001教室",
    "building_id": 21,
    "building_name": "逸夫楼",
    "garden_id": 9,
    "garden_name": "本校区",
    "headmaster": 42,
    "headmaster_name": "test",
    "headmaster_avatar": "http://mp.uxuj.test/uploads/20200109/2863d0a1ff4b87ba621cb84637c3796d.jpg",
    "city_name": "北京"
    }
    })
     */
    public function profile()
    {
        $id = $this->request->request('id');

        $classes = \app\common\model\Classes::alias('classes')
            ->join('__SCHOOL__ school', 'school.id = classes.school_id')
            ->join('__AREA__ area', 'area.id = school.city_id')
            ->join('__GRADE__ grade', 'grade.id = classes.grade_id')
            ->join('__CLASSROOM__ classroom', 'classroom.id = classes.classroom_id', 'left')
            ->join('__BUILDING__ building', 'building.id = classroom.building_id', 'left')
            ->join('__GARDEN__ garden', 'garden.id = building.garden_id', 'left')
            ->join('__TEACHER__ teacher', 'teacher.id = classes.headmaster', 'left')
            ->join('__USER__ user', 'user.id = teacher.user_id', 'left')
            ->field('classes.id,classes.name,classes.logo,classes.slogan,classes.grade_id,grade.name grade_name,classes.school_id,school.name school_name,school.logo school_logo,classes.classroom_id,classroom.name classroom_name,building.id building_id,building.name building_name,garden.id garden_id,garden.name garden_name,classes.headmaster,user.nickname headmaster_name,user.avatar headmaster_avatar,area.shortname city_name')
            ->where('classes.id', $id)
            ->find();

        if ($classes) {
            if (!$classes->logo) {
                $classes->logo = config('app_url') . '/assets/images/class_default_logo.png';
            }
            if (strpos($classes->school_logo, 'uploads/') > -1) {
                $classes->school_logo = config('app_url') . $classes->school_logo;
            }
            if (strpos($classes->headmaster_avatar, 'uploads/') > -1) {
                $classes->headmaster_avatar = config('app_url') . $classes->headmaster_avatar;
            }
        }

        $this->success('', $classes);
    }

    /**
     * @ApiTitle    (班级信息)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/2/6)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/classes/profiles)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="班级 ID", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function profiles()
    {
        $ids = $this->request->request('ids/a');

        $classes = \app\common\model\Classes::alias('classes')
            ->join('__SCHOOL__ school', 'school.id = classes.school_id')
            ->join('__AREA__ area', 'area.id = school.city_id')
            ->join('__GRADE__ grade', 'grade.id = classes.grade_id')
            ->join('__CLASSROOM__ classroom', 'classroom.id = classes.classroom_id', 'left')
            ->join('__BUILDING__ building', 'building.id = classroom.building_id', 'left')
            ->join('__GARDEN__ garden', 'garden.id = building.garden_id', 'left')
            ->join('__TEACHER__ teacher', 'teacher.id = classes.headmaster', 'left')
            ->join('__USER__ user', 'user.id = teacher.user_id', 'left')
            ->whereIn('classes.id', $ids)
            ->column('classes.id,classes.name,classes.logo,classes.slogan,classes.grade_id,grade.name grade_name,classes.school_id,school.name school_name,school.logo school_logo,classes.classroom_id,classroom.name classroom_name,building.id building_id,building.name building_name,garden.id garden_id,garden.name garden_name,classes.headmaster,user.nickname headmaster_name,user.avatar headmaster_avatar,area.shortname city_name');

        foreach ($classes as $key => $class) {
            if (!$class['logo']) {
                $classes[$key]['logo'] = config('app_url') . '/assets/images/class_default_logo.png';
            }
            if (strpos($class['school_logo'], 'uploads/') > -1) {
                $classes[$key]['school_logo'] = config('app_url') . $class['school_logo'];
            }
            if (strpos($class['headmaster_avatar'], 'uploads/') > -1) {
                $classes[$key]['headmaster_avatar'] = config('app_url') . $class['headmaster_avatar'];
            }
        }

        $this->success('', $classes);
    }

    public function search()
    {
        $school_id = $this->request->request('school_id', 0);
        $keywords = $this->request->request('keywords', '');
        $page = $this->request->post('page', 1);

        if ($page) {
            $list = \app\common\model\Classes::alias('classes')
                ->join('__SCHOOL__ school', 'school.id = classes.school_id')
                ->join('__AREA__ area', 'area.id = school.city_id')
                ->join('__GRADE__ grade', 'grade.id = classes.grade_id')
                ->join('__CLASSROOM__ classroom', 'classroom.id = classes.classroom_id', 'left')
                ->join('__BUILDING__ building', 'building.id = classroom.building_id', 'left')
                ->join('__GARDEN__ garden', 'garden.id = building.garden_id', 'left')
                ->join('__TEACHER__ teacher', 'teacher.id = classes.headmaster', 'left')
                ->join('__USER__ user', 'user.id = teacher.user_id', 'left')
                ->where('classes.school_id', $school_id)
                ->whereLike('classes.name', "%$keywords%")
                ->field('classes.id,classes.name,classes.logo,classes.slogan,classes.grade_id,grade.name grade_name,classes.school_id,school.name school_name,school.logo school_logo,classes.classroom_id,classroom.name classroom_name,building.id building_id,building.name building_name,garden.id garden_id,garden.name garden_name,classes.headmaster,user.nickname headmaster_name,user.avatar headmaster_avatar,area.shortname city_name')
                ->paginate();

        } else {
            $list = \app\common\model\Classes::alias('classes')
                ->join('__SCHOOL__ school', 'school.id = classes.school_id')
                ->join('__AREA__ area', 'area.id = school.city_id')
                ->join('__GRADE__ grade', 'grade.id = classes.grade_id')
                ->join('__CLASSROOM__ classroom', 'classroom.id = classes.classroom_id', 'left')
                ->join('__BUILDING__ building', 'building.id = classroom.building_id', 'left')
                ->join('__GARDEN__ garden', 'garden.id = building.garden_id', 'left')
                ->join('__TEACHER__ teacher', 'teacher.id = classes.headmaster', 'left')
                ->join('__USER__ user', 'user.id = teacher.user_id', 'left')
                ->where('classes.school_id', $school_id)
                ->whereLike('classes.name', "%$keywords%")
                ->column('classes.id,classes.name,classes.logo,classes.slogan,classes.grade_id,grade.name grade_name,classes.school_id,school.name school_name,school.logo school_logo,classes.classroom_id,classroom.name classroom_name,building.id building_id,building.name building_name,garden.id garden_id,garden.name garden_name,classes.headmaster,user.nickname headmaster_name,user.avatar headmaster_avatar,area.shortname city_name');
        }
        $this->success('', $list);
    }
}
